<template>
  <div class="shangjia-page">
    <div class="header-container">
      <IndexHeader></IndexHeader>
    </div>
    <div class="main-container">
      <el-card class="box-card">
        <div slot="header" class="clearfix">
          <span>商户中心</span>
        </div>
        
        <el-tabs v-model="activeTab">
          <el-tab-pane label="个人信息" name="info">
            <el-form :model="userInfo" :rules="rules" ref="userForm" label-width="100px">
              <el-form-item label="商户账号" prop="shangjiazhanghao">
                <el-input v-model="userInfo.shangjiazhanghao" disabled></el-input>
              </el-form-item>
              <el-form-item label="商户名称" prop="shangjiamingcheng">
                <el-input v-model="userInfo.shangjiamingcheng"></el-input>
              </el-form-item>
              <el-form-item label="联系电话" prop="lianxidianhua">
                <el-input v-model="userInfo.lianxidianhua"></el-input>
              </el-form-item>
              <el-form-item label="邮箱" prop="youxiang">
                <el-input v-model="userInfo.youxiang"></el-input>
              </el-form-item>
              <el-form-item label="头像">
                <el-upload
                  class="avatar-uploader"
                  :action="uploadUrl"
                  :headers="headers"
                  :show-file-list="false"
                  :on-success="handleAvatarSuccess">
                  <img v-if="userInfo.touxiang" :src="userInfo.touxiang" class="avatar">
                  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="updateInfo">保存修改</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>
          
          <el-tab-pane label="商品管理" name="products">
            <el-tabs v-model="productTab">
              <el-tab-pane label="笔记本" name="bijiben">
                <product-list 
                  type="bijiben"
                  :shangjiaId="userInfo.id || ''">
                </product-list>
              </el-tab-pane>
              <el-tab-pane label="电脑主机" name="diannaozhuji">
                <product-list 
                  type="diannaozhuji"
                  :shangjiaId="userInfo.id || ''">
                </product-list>
              </el-tab-pane>
              <el-tab-pane label="电脑硬件" name="yingjian">
                <product-list 
                  type="yingjianzuzhuang"
                  :shangjiaId="userInfo.id || ''">
                </product-list>
              </el-tab-pane>
              <el-tab-pane label="电脑外设" name="waishe">
                <product-list 
                  type="diannaowaishe"
                  :shangjiaId="userInfo.id || ''">
                </product-list>
              </el-tab-pane>
            </el-tabs>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import ProductList from './components/ProductList'
import config from '@/config/config'
import IndexHeader from '@/components/index/IndexHeader'

export default {
  name: 'Center',  // 添加组件名称
  components: {
    ProductList,
    IndexHeader  // 确保在components中注册
  },
  data() {
    return {
      activeTab: 'info',
      productTab: 'bijiben',
      userInfo: {
        id: '', // 添加默认值
        shangjiazhanghao: '',
        shangjiamingcheng: '',
        lianxidianhua: '',
        youxiang: '',
        touxiang: ''
      },
      uploadUrl: config.baseUrl + 'file/upload',
      headers: {
        Token: localStorage.getItem('Token')
      },
      rules: {
        shangjiamingcheng: [
          { required: true, message: '请输入商户名称', trigger: 'blur' }
        ],
        lianxidianhua: [
          { required: true, message: '请输入联系电话', trigger: 'blur' }
        ],
        youxiang: [
          { required: true, message: '请输入邮箱', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getUserInfo()
  },
  methods: {
    getUserInfo() {
      this.$http.get('shangjia/info').then(({data}) => {
        if (data && data.code === 0) {
          this.userInfo = data.data
        }
      })
    },
    handleAvatarSuccess(res) {
      if (res.code === 0) {
        this.userInfo.touxiang = res.file
      }
    },
    updateInfo() {
      this.$refs.userForm.validate(valid => {
        if (valid) {
          this.$http.post('shangjia/update', this.userInfo).then(({data}) => {
            if (data && data.code === 0) {
              this.$message.success('更新成功')
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>

<style scoped>
.shangjia-page {
  min-height: 100vh;
  background-color: #f0f2f5;
}
.header-container {
  background-color: #fff;
  box-shadow: 0 1px 4px rgba(0,21,41,.08);
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 1000;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.main-container {
  padding: 20px;
  margin-top: 64px; /* 为头部留出空间 */
}
.avatar-uploader {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  width: 178px;
  height: 178px;
}
.avatar-uploader:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>